<template>
  <div class="address">
    <el-breadcrumb separator="/">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item><a href="#">地址管理</a></el-breadcrumb-item>
      <el-breadcrumb-item>地址列表</el-breadcrumb-item>
      <el-breadcrumb-item>地址编辑</el-breadcrumb-item>
    </el-breadcrumb>

    <el-card class="box-card" style="margin-top: 25px">
      <el-form
        ref="ruleFormRef"
        :model="tableData"
        :rules="rules"
        label-width="120px"
        class="demo-ruleForm"
        :size="formSize"
        status-icon
      >
        <el-form-item label="姓名" prop="name">
          <el-input v-model="tableData.name" />
        </el-form-item>

        <el-form-item label="年龄" prop="age">
          <el-select-v2
            v-model="tableData.age"
            placeholder="Activity count"
            :options="options"
          />
        </el-form-item>
        <el-form-item label="日期" required>
          <el-col :span="11">
            <el-form-item prop="date">
              <el-date-picker
                v-model="tableData.date"
                format="YYYY-MM-DD"
                value-format="YYYY-MM-DD"
                style="width: 100%"
              />
            </el-form-item>
          </el-col>
        </el-form-item>

        <el-form-item label="手机号" prop="phone">
          <el-input v-model="tableData.phone" />
        </el-form-item>

        <el-form-item label="邮箱" prop="email">
          <el-input v-model="tableData.email" />
        </el-form-item>

        <el-form-item label="地址" prop="addresslist">
          <el-input v-model="tableData.addresslist" />
        </el-form-item>

        <el-form-item>
          <el-button type="primary" @click="submitForm(ruleFormRef)"
            >提交</el-button
          >
          <el-button @click="$router.go(-1)">返回</el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<script setup >
import axios from "axios";
import { ref, reactive, onMounted } from "vue";
import { useRouter, useRoute } from "vue-router";
import { address_edit } from "../../utils/api";
var $router = useRouter();
var $route = useRoute();

const formSize = ref("default");
const ruleFormRef = ref();
const tableData = ref({});

const rules = reactive({
  name: [{ required: true, message: "请输入姓名", trigger: "blur" }],
  age: [
    {
      required: true,
      message: "请输入年龄",
      trigger: "change",
    },
  ],
  date: [
    {
      required: true,
      message: "请填写日期",
      trigger: "change",
    },
  ],
  phone: [{ required: true, message: "请输入手机号", trigger: "change" }],
  email: [
    {
      required: true,
      message: "请输入邮箱",
      trigger: "change",
    },
  ],
  addresslist: [
    {
      required: true,
      message: "请输入地址",
      trigger: "change",
    },
  ],
});

const submitForm = async (formEl) => {
  if (!formEl) return;
  await formEl.validate((valid, fields) => {
    if (valid) {
      address_edit({ query: JSON.stringify(tableData.value) }).then((res) => {
        if (res.data.code == 200) {
          ElMessage({
            showClose: true,
            message: res.data.msg,
            type: "success",
            duration: 1000,
          });
          $router.push("/index/address");
        } else {
          ElMessage({
            showClose: true,
            message: res.data.msg,
            type: "warning",
            duration: 1000,
          });
        }
      });
    } else {
      console.log("error submit!", fields);
    }
  });
};

const options = Array.from({ length: 10000 }).map((_, idx) => ({
  value: `${idx + 1}`,
  label: `${idx + 1}`,
}));

onMounted(() => {
  // console.log($route.query.rowlist);
  var query = $route.query.rowlist;
  // console.log(query);
  tableData.value = JSON.parse(query);
  // console.log(tableData.value);
});
</script>

<style  lang="scss" scoped>
// .title {
//   height: 20px;
//   /* font-weight: bold; */
//   line-height: 20px;
//   text-align: center;
//   margin: 10px;
//   .el-input__inner {
//     width: 50%;
//   }
// }

.el-input {
  width: 50%;
}
.tianjia {
  margin-left: 50px;
}
.good-container {
  min-height: 100%;
}
.el-card.is-always-shadow {
  min-height: 100% !important;
}

.el-table .el-button {
  width: 70px;
  height: 30px;
  margin: 5px 0;
  margin-right: 20px !important;
  color: black;
  border: 1px solid #4caf50;
  text-align: center;
  line-height: 30px;
  &:nth-child(2) {
    border: 1px solid #4caf50;
  }
}
.el-table .el-button:hover {
  background-color: #4caf50;
  color: white;
  &:nth-child(2) {
    background-color: #4caf50;
  }
}
.el-message-box__status {
  position: absolute !important;
}
.xiabtn {
  border: 1px solid lightcoral !important;
}
.xiabtn:hover {
  background-color: lightcoral !important;
}
</style>